package com.bestcem.xm.user.grpc.service.converter;

import com.bestcem.bp.xm.user.grpc.v1.services.CreateOrgWalletConfigRequest;
import com.bestcem.bp.xm.user.grpc.v1.services.OrgWalletConfig;
import com.bestcem.bp.xm.user.grpc.v1.services.UpdateOrgWalletConfigRequest;
import com.bestcem.xm.user.grpc.service.converter.strategy.UserEnumStrategy;
import com.bestcem.xm.user.service.dto.organization.OrganizationWalletConfigDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 公司钱包配置转换类
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/29 15:10
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OrgWalletConfigGrpcConverter {

    public static OrgWalletConfig dto2GetOrgWalletConfigGrpc(OrganizationWalletConfigDTO orgWalletConfigDTO) {
        if ( orgWalletConfigDTO == null ) {
            return null;
        }

        OrgWalletConfig.Builder orgWalletConfig = OrgWalletConfig.newBuilder();

        if ( orgWalletConfigDTO.getOrgId() != null ) {
            orgWalletConfig.setOrgId( orgWalletConfigDTO.getOrgId() );
        }
        if ( orgWalletConfigDTO.getRechargeLimit() != null ) {
            orgWalletConfig.setRechargeLimit( UserEnumStrategy
                    .orgWalletConfigRechargeLimitConveter( orgWalletConfigDTO.getRechargeLimit() ) );
        }

        return orgWalletConfig.build();
    }

    public static OrganizationWalletConfigDTO createOrgWalletConfigGrpc2Dto(CreateOrgWalletConfigRequest request) {
        if ( request == null ) {
            return null;
        }

        OrganizationWalletConfigDTO organizationWalletConfigDTO = new OrganizationWalletConfigDTO();

        organizationWalletConfigDTO.setRechargeLimit( request.getRechargeLimitValue() );
        if ( request.getOrgId() != null ) {
            organizationWalletConfigDTO.setOrgId( request.getOrgId() );
        }

        return organizationWalletConfigDTO;
    }

    public static OrganizationWalletConfigDTO updateOrgWalletConfigGrpc2Dto(UpdateOrgWalletConfigRequest request) {
        if ( request == null ) {
            return null;
        }

        OrganizationWalletConfigDTO organizationWalletConfigDTO = new OrganizationWalletConfigDTO();

        organizationWalletConfigDTO.setRechargeLimit( request.getRechargeLimitValue() );
        if ( request.getOrgId() != null ) {
            organizationWalletConfigDTO.setOrgId( request.getOrgId() );
        }

        return organizationWalletConfigDTO;
    }

    public static List<OrgWalletConfig> dtos2GetOrgWalletConfigGrpcs(List<OrganizationWalletConfigDTO> orgWalletConfigDTOList) {
        if ( orgWalletConfigDTOList == null ) {
            return Collections.emptyList();
        }

        List<OrgWalletConfig> list = new ArrayList<>( orgWalletConfigDTOList.size() );
        for ( OrganizationWalletConfigDTO organizationWalletConfigDTO : orgWalletConfigDTOList ) {
            list.add( dto2GetOrgWalletConfigGrpc( organizationWalletConfigDTO ) );
        }

        return list;
    }
}
